package cn.bobohost.crmrbac.web.interceptor;

import cn.bobohost.crmrbac.pojo.po.Employee;
import cn.bobohost.crmrbac.util.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class CheckLoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("-----------------------经过了登录拦截器。。。");
        //检查: 到会话中后去是否有这个key的用户
        Employee employee = UserContext.getEmployee();
        if (employee != null){
            //放行，继续执行Controller
            return true;
        }

        //否则，跳转到登录
        response.sendRedirect("/login.html");
        return false;
    }
}
